import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

Vue.use(VueRouter)


export const IndexRoutes = [{
        path: "menu",
        component: () =>
            import ("../pages/menu/menu.vue"),
        meta: [{
            title: "菜单管理"
        }],
    },
    {
        path: "menu/add",
        component: () =>
            import ("../pages/menu/components/add.vue"),
        meta: [{
            title: "菜单管理",
            url: '/menu'
        }, {
            title: "菜单添加"
        }]
    }, {
        path: "menu/edit/:id",
        component: () =>
            import ("../pages/menu/components/add.vue"),
        meta: [{
            title: "菜单管理",
            url: '/menu'
        }, {
            title: "菜单修改"
        }]
    },
    {
        path: "role",
        component: () =>
            import ("../pages/role/role.vue"),
        meta: [{
            title: "角色管理"
        }]
    },
    {
        path: "role/add",
        component: () =>
            import ("../pages/role/components/add.vue"),
        meta: [{
            title: "角色管理",
            url: '/role'
        }, {
            title: "角色添加"
        }]
    }, {
        path: "role/edit/:id",
        component: () =>
            import ("../pages/role/components/add.vue"),
        meta: [{
            title: "角色管理",
            url: '/role'
        }, {
            title: "角色修改"
        }]
    }, {
        path: "user",
        component: () =>
            import ("../pages/manage/manage.vue"),
        meta: [{
            title: "管理员管理"
        }]
    }, {
        path: "user/add",
        component: () =>
            import ("../pages/manage/components/add.vue"),
        meta: [{
            title: "管理员管理",
            url: '/user'
        }, {
            title: "管理员添加"
        }]
    }, {
        path: "user/edit/:id",
        component: () =>
            import ("../pages/manage/components/add.vue"),
        meta: [{
            title: "管理员管理",
            url: '/user'
        }, {
            title: "管理员修改"
        }]
    },
    {
        path: "category",
        component: () =>
            import ("../pages/cate/cate.vue"),
        meta: [{
            title: "商品分类"
        }]
    }, {
        path: "category/add",
        component: () =>
            import ("../pages/cate/components/add.vue"),
        meta: [{
            title: "商品分类",
            url: '/category'
        }, {
            title: "分类添加"
        }]
    }, {
        path: "category/edit/:id",
        component: () =>
            import ("../pages/cate/components/add.vue"),
        meta: [{
            title: "商品分类",
            url: '/category'
        }, {
            title: "分类修改"
        }]
    },
    {
        path: "specs",
        component: () =>
            import ("../pages/specs/specs.vue"),
        meta: [{
            title: "商品规格"
        }]
    }, {
        path: "specs/add",
        component: () =>
            import ("../pages/specs/components/add.vue"),
        meta: [{
            title: "商品规格",
            url: '/specs'
        }, {
            title: "规格添加"
        }]
    }, {
        path: "specs/edit/:id",
        component: () =>
            import ("../pages/specs/components/add.vue"),
        meta: [{
            title: "商品规格",
            url: '/specs'
        }, {
            title: "规格修改"
        }]
    },
    {
        path: "goods",
        component: () =>
            import ("../pages/goods/goods.vue"),
        meta: [{
            title: "商品管理"
        }]
    }, {
        path: "goods/add",
        component: () =>
            import ("../pages/goods/components/add.vue"),
        meta: [{
            title: "商品管理",
            url: '/goods'
        }, {
            title: "商品添加"
        }]
    }, {
        path: "goods/edit/:id",
        component: () =>
            import ("../pages/goods/components/add.vue"),
        meta: [{
            title: "商品管理",
            url: '/goods'
        }, {
            title: "商品修改"
        }]
    },
    {
        path: "banner",
        component: () =>
            import ("../pages/banner/banner.vue"),
        meta: [{
            title: "轮播图管理"
        }]
    }, {
        path: "banner/add",
        component: () =>
            import ("../pages/banner/components/add.vue"),
        meta: [{
            title: "轮播图管理",
            url: '/banner'
        }, {
            title: "轮播图添加"
        }]
    }, {
        path: "banner/edit/:id",
        component: () =>
            import ("../pages/banner/components/add.vue"),
        meta: [{
            title: "轮播图管理",
            url: '/banner'
        }, {
            title: "轮播图修改"
        }]
    },
    {
        path: "member",
        component: () =>
            import ("../pages/member/member.vue"),
        meta: [{
            title: "会员管理"
        }]
    }, {
        path: "member/edit/:id",
        component: () =>
            import ("../pages/member/components/add.vue"),
        meta: [{
            title: "会员管理",
            url: '/member'
        }, {
            title: "会员修改"
        }]
    },
    {
        path: "seckill",
        component: () =>
            import ("../pages/seckill/seckill.vue"),
        meta: [{
            title: "秒杀活动"
        }]
    },{
        path: "seckill/add",
        component: () =>
            import ("../pages/seckill/components/add.vue"),
        meta: [{
            title: "秒杀活动"
        },{
            title: "活动添加"
        }
        ]
    }, {
        path: "seckill/edit/:id",
        component: () =>
            import ("../pages/seckill/components/add.vue"),
        meta: [{
            title: "秒杀活动",
            url: '/seckill'
        }, {
            title: "活动修改"
        }]
    }
];

const routes = [{
    path: '/login',
    component: () =>
        import ('../pages/login/login.vue')
}, {
    path: '/',
    component: () =>
        import ('../pages/index/index.vue'),

    children: [{
            path: "",
            component: () =>
                import ("../pages/home/home.vue"),
            meta: []

        },
        ...IndexRoutes
    ]
}]

const router = new VueRouter({
    routes
})

// 设置路由权限
IndexRoutes.forEach(item => {
    item.beforeEnter = (to, from, next) => {
        if (store.getters.user_info.menus_url.includes('/' + item.path)) {
            next();
        } else {
            next('/');
        }
    }
})

// 登陆拦截
router.beforeEach((to, from, next) => {

    if (to.path === '/login') {
        next();
        return
    }

    if (store.getters.user_info.token) {
        // 如果vuex有用户信息本地存储没有,存到本地
        if (!sessionStorage.getItem('user_info')) {
            sessionStorage.setItem('user_info', JSON.stringify(store.getters.user_info));
        }
        next();
        return
    } else if (sessionStorage.getItem('user_info')) {
        // 如果vuex没有用户信息而本地存储有值,存到vuex
        store.commit('set_userInfo', JSON.parse(sessionStorage.getItem('user_info')));
        next();
        return
    }

    next('/login')
})

export default router